<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\Level;
use Illuminate\Http\Request;

class LevelController extends Controller
{
    public function index()
    {
        $result = [
            'headhunting_commission_level'=>Level::index()->get()
        ];

        return view('admin.level.index', $result);
    }

    public function add()
    {
        $result = [
            'headhunting_commission_level'=>Level::index()->orderBy('rank_min','desc')->get()
        ];

        return view('admin.level.edit', $result);
    }

    public function edit(Request $request, $id)
    {
        $result = [
            'id'=>$id,
            'result'=> Level::info($id),
        ];

        return view( 'admin.level.edit', $result);
    }

    public function do(Request $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            try {

                if (empty($input['id'])) {
                    unset($input['id']);
                    Level::add($input);
                } else {
                    Level::edit($input['id'],$input);
                }

            }catch (\Exception $e){
                return self::fail($e->getMessage());
            }

            return self::success([]);
        }
    }

    public function del($id)
    {

        Level::del($id);

        return self::success([]);

    }
}
